package idplib

import (
	"bufio"
	"encoding/json"
	"fmt"
	"github.com/urfave/cli/v2"
	"os"
)

var (
	BuildTime string
	Version   = "1.0.0"
)

func Do() {
	app := &cli.App{
		Name:      "idp",
		Version:   fmt.Sprintf("%s build:%s", Version, BuildTime),
		Usage:     "idp v1 版本信息采集插件，仅适用于v1到v2插件版过渡期间。",
		UsageText: "eg: idp.exe -code=num  code值参考旧版本指令号\n idp.exe -code=2,获取主板信息 \n ",
		Flags: []cli.Flag{
			&cli.IntFlag{
				Name:     "code",
				Usage:    "指令号",
				Required: true,
			},
		},
		Action: func(ctx *cli.Context) error {
			code := ctx.Int("code")
			var (
				bs []byte
			)
			switch code {
			case CODE_GET_BASE_BOARD:
				data := GetBaseBoardInfo()
				bs, _ = json.Marshal(data)
			case CODE_GET_COMPUTER_ABSTRACT:
				data := GetSystemAbstractInfo()
				bs, _ = json.Marshal(data)
			case CODE_DISK:
				data := GetDiskInfo()
				bs, _ = json.Marshal(data)
			case CODE_LOGIC_DISK:
				data := GetLogicDiskInfo()
				bs, _ = json.Marshal(data)
			case CODE_MEMORY:
				data := GetMemoryInfo()
				bs, _ = json.Marshal(data)
			case CODE_NETWORK_MAC:
				r, b := GetActiveNetWorkInfo()
				if b {
					bs, _ = json.Marshal(r)
				}
			case CODE_NETWORK_IP:
				r, b := GetActiveIP()
				if b {
					bs, _ = json.Marshal(r)
				}
			case CODE_NETWORK:
				r, r1 := GetAllNetWorkInfo()
				r = append(r, r1...)
				bs, _ = json.Marshal(r)
			case CODE_OPERATING_SYSTEM:
				data := GetOperatingSystem()
				bs, _ = json.Marshal(data)
			case CODE_PROCESOR:
				data := GetCpuInfo()
				bs, _ = json.Marshal(data)
			case CODE_PROCESS:
				data := GetProcessInfo()
				bs, _ = json.Marshal(data)
			case CODE_SERVICE:
				data := GetService()
				bs, _ = json.Marshal(data)
			case CODE_STARTUP_COMMAND:
				data := GetStartupCommandInfo()
				bs, _ = json.Marshal(data)
			case CODE_SOFTWARE:
				data := GetSoftWallInfo()
				bs, _ = json.Marshal(data)
			case CODE_SERVER_PACKAGEINFO:
				data := GetServicePackInfo()
				bs, _ = json.Marshal(data)
			case CODE_DESKTOP_MONITOR:
				data := GetDisplayInfo()
				bs, _ = json.Marshal(data)
			case CODE_DESKTOP_BY_LOGICAL_DEVICE:
				data := GetDisplayInfoByLogicalDevice()
				bs, _ = json.Marshal(data)
			case CODE_BIOS_CODE:
				data := GetBiosInfo()
				bs, _ = json.Marshal(data)
			default:
				return cli.Exit(fmt.Sprintf("计划之外的指令码：%d", code), 1)
			}
			w := bufio.NewWriter(os.Stdout)
			w.WriteString(string(bs) + "\n")
			w.Flush()
			return nil
		},
	}
	if err := app.Run(os.Args); err != nil {
		fmt.Printf("命令行调用错误：%s ，请查看帮助文档", err.Error())
	}
}
